<?php

namespace app\admin\model;

use app\model\Model;

class Sitemap extends Model
{
  public $str;
  public $db;

  function __construct()
  {
    $this->db = $GLOBALS['db'];
    $size = iif($_GET['size'], 100);
    $arr = $this->db->getAll("SELECT id FROM channel");
    $arr = get_easy_array($arr, 'id');

    $res = list_detail(implode(',', $arr), '0,' . $size);
    unset($arr);

    // header('Content-type: application/xml; charset="UTF-8"', true);
    //"always", "hourly", "daily", "weekly", "monthly", "yearly" and "never"
    $changefreq = "weekly";
    $this->str = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
$this->str .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . PHP_EOL;
  foreach ($res as $val) {
  $this->str .= '<url>' . PHP_EOL .
    '<loc>' . $GLOBALS['cms']['domain'] . '/' . htmlspecialchars(d_url($val['id'])) . '</loc>' . PHP_EOL .
    '<lastmod>' . local_date("Y-m-d H:i:s", $val['d_date']) . '</lastmod>' . PHP_EOL .
    '<changefreq>' . $changefreq . '</changefreq>' . PHP_EOL .
    '<priority>0.8</priority>' . PHP_EOL .
    '</url>' . PHP_EOL;
  }
  $this->str .= '</urlset>';
unset($res);
}

function index()
{
header("Content-type:text/xml");
echo $this->str;
}

function file()
{
file_it($this->str, '/', 'sitemap.xml');
alert_back($GLOBALS['lang']['msg_success']);
}

function __destruct()
{
}
}